﻿using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using System.Windows.Forms;
using Native.XQ.HIni.Tool;

namespace Drawastick.UI
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            var ini = new IniObject(Caller.API.AppDirectory + "签.ini");
            ini.Load();
            if (ini["是否开启"]["分群"] == "1")
            {
                checkBox1.Checked = true;
            }
            else
            {
                checkBox1.Checked = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var ini = new IniObject(Caller.API.AppDirectory + "签.ini");
            ini.Load();
            if (checkBox1.Checked)
            {
                ini["是否开启"]["分群"] = "1";
            }
            else
            {
                ini["是否开启"]["分群"] = "0";
            }
            ini.Save();
            MessageBox.Show("保存成功，及时生效");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Task.Factory.StartNew(async () =>
            {
                if (File.Exists(Caller.API.AppDirectory + "签.ini"))
                {
                    var r = MessageBox.Show("签.ini已存在,确定要覆盖吗?", "重要", MessageBoxButtons.YesNo);

                    if (r != DialogResult.Yes)
                    {
                        return;
                    }
                }
                var str = await new HttpClient().GetAsync("https://gitee.com/heerkaisair/Draw-A-Stick/raw/master/%E7%AD%BE.ini");
                File.Create(Caller.API.AppDirectory + "签.ini").Close();
                File.WriteAllText(Caller.API.AppDirectory + "签.ini", await str.Content.ReadAsStringAsync());

                MessageBox.Show("下载写入完毕");
            });
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}